home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11558 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  111 lines

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: is getopt() ANSI and portable ?
  5. Date: 22 Mar 96 01:50:05 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.827459405@hubcap>
  8. References: <31512EC7.389F@eso.org>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10. X-Newsreader: NN version 6.5.0 #1
  11.  
  12. Nicolas Devillard <nDevil@eso.org> writes:
  13.  
  14. >I do not want to reinvent the wheel by creating again my own
  15. >command line parser. I found this getopt() function being part
  16. >of stdlib, but I'd like to know:
  17.  
  18. >1. If it is part of the ANSI C stdlib ?
  19.  
  20. No, it's not.
  21.  
  22. >2. If it is a portable call ? I cannot find it in the POSIX prog guide.
  23.  
  24. Mark Horton's 1990 _Portable C Programming_ book rates it as only
  25. moderately portable.  It's a System III and System V Unix function,
  26. but is not common elsewhere.
  27.  
  28. Attached is a public-domain version from AT&T (as published in Appendix F
  29. of Horton).  No warranty, express or implied, etc., etc., etc.
  30.  
  31.         Matthew Saltzman
  32.         Clemson University Math Sciences
  33.         mjs@clemson.edu
  34.  
  35. -------------------------getopt.c-----------------------------
  36. /*LINTLIBRARY*/
  37. #define NULL       0
  38. #define EOF (-1)
  39. #define ERR(s, c)  if(opterr) {\
  40.       extern int strlen(), _write();\
  41.       char errbuf[2];\
  42.       errbuf[0] = c; errbuf[1] = '\n';\
  43.       (void) _write(2, argv[0], (unsigned)strlen(argv[0]));\
  44.       (void) _write(2, s, (unsigned)strlen(s));\
  45.       (void) _write(2, errbuf, 2);}
  46.  
  47. extern int strcmp();
  48. extern char *strchr();
  49. /*
  50.  * If building the regular library, pick up the definitions
  51.  * from this file.  If building the shared library, pick up
  52.  * definitions from opt_data.c
  53.  */
  54.  
  55. extern int opterr, optind, optopt;
  56. extern char *optarg;
  57.  
  58. static char error1[] = ": option requires an argument -- ";
  59. static char error2[] = ": illegal option -- ";
  60.  
  61. int
  62. getopt(argc, argv, opts)
  63. int   argc;
  64. char  **argv, *opts;
  65. {
  66.       static int sp = 1;
  67.       register char c;
  68.       register char *cp;
  69.  
  70.       if(sp == 1)
  71.         if(optind >= argc ||
  72.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  73.               return(EOF);
  74.         else if(strcmp(argv[optind], "--") == NULL) {
  75.               optind++;
  76.           return(EOF);
  77.         }
  78.       optopt = c = (unsigned char)argv[optind][sp];
  79.       if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  80.         ERR(error2,c);
  81.         if(argv[optind][++sp] == '\0') {
  82.               optind++;
  83.           sp = 1;
  84.         }
  85.         return('?');
  86.       }
  87.       if(*++cp == ':') {
  88.         if(argv[optind][sp+1] != '\0')
  89.               optarg = &argv[optind++][sp+1];
  90.         else if(++optind >= argc) {
  91.               ERR(error1,c);
  92.           sp = 1;
  93.           return('?');
  94.         } else
  95.               optarg = argv[optind++];
  96.         sp = 1;
  97.       } else {
  98.         if(argv[optind][++sp] == '\0') {
  99.               sp = 1;
  100.           optind++;
  101.         }
  102.         optarg = NULL;
  103.       }
  104.       return(c);
  105. }
  106. ------------------------------------------
  107. -- 
  108.         Matthew Saltzman
  109.         Clemson University Math Sciences
  110.         mjs@clemson.edu
  111.